package com.animal.service.impl;

import com.animal.entity.*;
import com.animal.mapper.AnCarMapper;
import com.animal.mapper.AnProductMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.service.AnCarService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@Service
public class AnCarServiceImpl extends ServiceImpl<AnCarMapper, AnCar> implements AnCarService {
    @Autowired
    AnCarMapper anCarMapper;
    @Autowired
    AnUserMapper anUserMapper;
    @Autowired
    AnProductMapper anProductMapper;

    public List<AnCar> findAll(int size,int current) {
        Page<AnCar> userPages = new Page<>(size,current);
        Page<AnCar> userPagess=  anCarMapper.selectPage(userPages,null);
        List<AnCar> list=userPagess.getRecords();
        System.out.println("++++++++++++++++++++++++++");
        System.out.println(list);
        for(AnCar a:list){
            a.setAnUser(anUserMapper.selectById(a.getUserId()));
            a.setAnProduct(anProductMapper.selectById(a.getPrId()));
        }
        return list;
    }

    @Override
    public List<AnCar> findByUserId(int size, int current, int userId) {
        Page<AnCar> userPages = new Page<>(size,current);
        QueryWrapper<AnCar> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AnCar> userPagess=  anCarMapper.selectPage(userPages,wrapper);
        List<AnCar> list=userPagess.getRecords();
        System.out.println(list);
        for(AnCar a:list){
            a.setAnUser(anUserMapper.selectById(a.getUserId()));
            a.setAnProduct(anProductMapper.selectById(a.getPrId()));
        }
        return list;
    }
}
